﻿using HartConfigTool.HARTCommands.CMDs;
using HartConfigTool.SerialPortUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HartConfigTool.UI.DebugWindow
{
    internal sealed class CMDsDebugWinHARTCMD14 : CMD14ReadPVTransducerInfo
    {
        public CMDsDebugWinHARTCMD14(SerialPortsManager serialPortManager) : base(serialPortManager) { }

        protected override async Task SendCMDCode()
        {
            byte[] tempAry = new byte[16];
            byte[] cmdCodeBytes = StructToBytes<CMD14ReadPVTransInfoCodeStruct>(CMD14ReadPVTransInfoCode);

            Array.Copy(cmdCodeBytes, 5, tempAry, 0, 8);
            cmdCodeBytes[cmdCodeBytes.Length - 1] = HARTProtocolV7.GetCRC(tempAry, (byte)8);

            serialPortManager.DebugMessage("[" + DateTime.Now + "]:-->" + HARTProtocolV7.HexToString(cmdCodeBytes) + "\r\n");

            await serialPortManager.SendCMD(cmdCodeBytes);
        }
        public override async Task ParseResponse(byte[] responseData)
        {
            await Task.Run(() =>
            {
                List<string> parts = new List<string>();
                byte[] bytes = null;
                byte[] data = null;

                data = serialPortManager.GetRxBufferData();

                bytes = HARTProtocolV7.GetDataFrame(data);

                // Debug Message
                serialPortManager.DebugMessage("[" + DateTime.Now + "]:<--" + HARTProtocolV7.HexToString(data) + "\r\n");

                // Transducer Serial Number
                parts.Clear();
                byte[] transInfo = new byte[4];
                transInfo[0] = 0;
                Array.Copy(bytes, 10, transInfo, 1, 3);
                Array.Reverse(transInfo);
                parts.Add((BitConverter.ToInt32(transInfo, 0)).ToString());
                NotifyObservers(CMD14Observers, ObserversInfo.HARTCMD14Observers["Transducer SN"], parts);
                // Transducer Limit and Mini Span Units Code
                parts.Clear();
                parts.Add("0x" + bytes[13].ToString("X2"));
                NotifyObservers(CMD14Observers, ObserversInfo.HARTCMD14Observers["Transducer Li And Mini Span Unit"], parts);
                // Upper Transducer Limit
                parts.Clear();
                parts.Add((BitConverter.ToSingle(bytes, 14)).ToString());
                NotifyObservers(CMD14Observers, ObserversInfo.HARTCMD14Observers["Upper Transducer Limit"], parts);
                // Lower Transducer Limit
                parts.Clear();
                parts.Add((BitConverter.ToSingle(bytes, 18)).ToString());
                NotifyObservers(CMD14Observers, ObserversInfo.HARTCMD14Observers["Lower Transducer Limit"], parts);
                // Minimum Span
                parts.Clear();
                parts.Add((BitConverter.ToSingle(bytes, 22)).ToString());
                NotifyObservers(CMD14Observers, ObserversInfo.HARTCMD14Observers["Mini Span"], parts);

                this.serialPortManager.RxDataClear();
            });
        }
    }
}
